using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

using AIFSDataAccessLayer;

using AIFSContentManagementSystem;
using AIFSContentManagementSystem.Articles;
using AIFSContentManagementSystem.Criteria;

using AIFSCMSDataAccessLayer.EntityMappers;
using AIFSCMSDataAccessLayer.ProxyModels;

namespace AIFSCMSDataAccessLayer.RepositoryImpl
{
    public class ArticleRepository : IArticleRepository
    {
        IDataAwareRepositoryMap dataAwareRepositoryMap;
        public IRepositoryMap RepositoryMap
        {
            get
            {
                return dataAwareRepositoryMap;
            }
        }

        private IDataExecutionContext DataExecutionContext
        {
            get { return dataAwareRepositoryMap.DataExecutionContext; }
        }

        public ArticleRepository(IDataAwareRepositoryMap RepositoryMap)
        {
            this.dataAwareRepositoryMap = RepositoryMap;
        }

        public List<Article> GetArticles(string articleTypeCode, string program, bool? activeFlag)
        {
            IDataExecutionContext dataContext = this.dataAwareRepositoryMap.DataExecutionContext;
            DalSqlCommand cmd = dataContext.CreateStoredProcCommand("GetArticles");

            cmd.CreateParameter("ARTICLE_TYPE_CODE", articleTypeCode);
            cmd.CreateParameter("PROGRAM", program);
            cmd.CreateParameter("ACTIVE_FLAG", activeFlag);

            DataTable dt = dataContext.ExecuteDataTable(cmd);

            ArticleMapper mapper = new ArticleMapper();
            
            List<ArticleProxy> proxies = mapper.Map(dt);
            
            //Return as List of Article
            List<Article> articles = new List<Article>();
            foreach (ArticleProxy proxy in proxies)
            {
                proxy.RepositoryMap = this.RepositoryMap;
                articles.Add(proxy);
            }

            return articles;
        }

        public List<ArticleCriterion> GetCriteriaForArticle(int? articleId)
        {
            DalSqlCommand cmd = this.DataExecutionContext.CreateStoredProcCommand("GetCriteriaForArticle");

            cmd.CreateParameter("ARTICLE_ID", articleId);

            DataTable dt = this.DataExecutionContext.ExecuteDataTable(cmd);
            
            ArticleCriteriaMapper mapper = new ArticleCriteriaMapper();
            List<ArticleCriterion> articleCriteria = mapper.Map(dt);
            return articleCriteria;
        }

        public List<ArticleFragment> GetFragmentForArticle(int? articleId)
        {
            DalSqlCommand cmd = this.DataExecutionContext.CreateStoredProcCommand("GetFragmentForArticle");

            cmd.CreateParameter("ARTICLE_ID", articleId);

            DataTable dt = this.DataExecutionContext.ExecuteDataTable(cmd);
            ArticleFragmentMapper mapper = new ArticleFragmentMapper();
            List<ArticleFragment> articleFragments = mapper.Map(dt);
            return articleFragments;
        }

        public Fragment GetFragment(int? fragmentId)
        {
            DalSqlCommand cmd = this.DataExecutionContext.CreateStoredProcCommand("GetFragment");

            cmd.CreateParameter("FRAGMENT_ID", fragmentId);

            DataTable dt = this.DataExecutionContext.ExecuteDataTable(cmd);

            if (dt == null || dt.Rows.Count == 0)
                return null;

            FragmentMapper mapper = new FragmentMapper();
            Fragment fragment = mapper.Map(dt.Rows[0]);
            return fragment;
        }
    }
}
